<?php


namespace App\Http\Controllers\Api;
use App\Exports\PositionExport;
use App\Models\Config;
use App\Models\Position;
use Dingo\Api\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class PositionController extends BaseController
{
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $company_id = $request->input('company_id');
        $city = $request->input('city');
        $type = $request->input('type');
        $limit = $request->get('per_page',20);

        $list = Position::with('company')->with('company.type')->where('status', 1)
        ->when($company_id, function ($q) use($company_id){
            $q->where('company_id', $company_id);
        })->when($city, function ($q) use($city){
            $q->where('city',  $city );
        })->when($type, function ($q) use($type){
            $q->where('type',  $type );
        })->select('position.*')->orderBy('position.sort', 'desc')
            ->orderBy("position.updated_at", "desc")
            ->paginate($limit);
        return $this->paginate($list);
    }

    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:position'
        ]);
        $id = \request("id");
        $info = Position::with('company')->with('company.type')->find($id);
        if (!$info) {
            return $this->failed('提示该职位已下线或该职位已删除');
        }
        return $this->success($info);
    }


}

